<?php
/**
 * User's Model
 *
 * @category    IPB_CMS
 * @package     Application_Model
 * @copyright   Copyright (c) 2009 IPB CMS <http://ipb-cms.googlecode.com/>
 * @license     http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License
 * @author      Henrique Moody <henriquemoody@gmail.com>
 */
class Model_User extends App_Model_Abstract
{

    /**
     * Id of the table
     *
     * @var integer
     */
    protected $_userId;

    /**
     * Date of creation
     *
     * @var Zend_Date
     */
    protected $_userCreated;

    /**
     * Date of updated
     *
     * @var Zend_Date
     */
    protected $_userUpdated;

    /**
     * Username of the user
     *
     * @todo Create getter and setter for it
     * @var string
     */
    protected $_userUsername;

    /**
     * Password of the user
     *
     * @todo Create getter and setter for it
     * @var string Hashed with US Secure Hash Algorithm 1
     */
    protected $_userPassword;

    /**
     * Nickname of the user
     *
     * @todo Create getter and setter for it
     * @var string
     */
    protected $_userNickname;

    /**
     * Creator's Model
     *
     * @var Model_User
     */
    protected $_creator;

    /**
     * Person's Model
     *
     * @var Model_Person
     */
    protected $_person;

    /**
     * Updater's Model
     *
     * @var Model_User
     */
    protected $_updater;

    /**
     * Sets the user id
     *
     * @param integer $id Id of the user
     * @return Model_User Fluent interface, returns self
     */
    public function setId($id)
    {
        $this->_userId = (integer) $id;
        return $this;
    }

    /**
     * Returns the user id
     *
     * @return integer Id of the user
     */
    public function getId()
    {
        return $this->_userId;
    }

    /**
     * Sets the date and time of creation
     *
     * @see Zend_Date
     * @param int|string $datetime Unix timestamp or date
     * @return Model_User Fluent interface, returns self
     */
    public function setCreated($datetime) {
        $this->_userCreated = new Zend_Date($datetime);
        return $this;
    }

    /**
     * Returns the date and time of creation
     *
     * @return Zend_Date
     */
    public function getCreated()
    {
        return $this->_userCreated;
    }

    /**
     * Sets the date and time of update (if any)
     *
     * @param string|integer|null $datetime Unix timestamp or date of update
     * @return Model_User Fluent interface, returns self
     */
    public function setUpdated($datetime = null)
    {
        if (!is_null($datetime)) {
            $this->_userUpdated = new Zend_Date($datetime);
        }
        return $this;
    }

    /**
     * Returns the date of last update (if any)
     *
     * @return Zend_Date|null
     */
    public function getUpdated()
    {
        return $this->_userUpdated;
    }

    /**
     * Sets the username of the user
     *
     * @param string $username
     * @return Model_User Fluent interface, returns self
     */
    public function setUsername($username)
    {
        $this->_userUsername = (string) $username;
        return $this;
    }

    /**
     * Returns the user's username
     *
     * @return string  Username of the user
     */
    public function setUsername()
    {
        return $this->_userUsername;
    }

    /**
     * Sets the user's password
     *
     * @param string $password
     * @return Model_User Fluent interface, returns self
     */
    public function setPassword($password)
    {
        if (strlen($password) != 41) {
            $password = sha1('IPB_SALT_KEY' . $password);
        }
        $this->_userPassword = (string) $password;
        return $this;
    }

    /**
     * Returns the user's password
     *
     * @return string User's password
     */
    public function getPassword()
    {
        return $this->_userPassword;
    }

    /**
     * Sets the user's nickname
     *
     * @param string $nickname
     * @return Model_User Fluent interface, returns self
     */
    public function setNickname($nickname)
    {
        $this->_userNickname = (string) $nickname;
        return $this;
    }

    /**
     * Returns the user's nickname
     *
     * @return string User's nickname
     */
    public function getNickname()
    {
        return $this->_userNickname;
    }

    /**
     * Sets the id of the creator
     *
     * @param integer|null $creatorId Id of the creator
     * @return Model_User Fluent interface, returns self
     */
    public function setCreatorId($creatorId = null) {
        if (!is_null($creatorId)) {
            $this->getCreator()->setId($creatorId);
        }
        return $this;
    }

    /**
     * Returns the id of the creator
     *
     * @return integer|null Id of the creator
     */
    public function getCreatorId()
    {
        return $this->getCreator()->getId();
    }

    /**
     * Sets the creator of the user
     *
     * @param Model_User $creator Creator of the user
     * @return Model_User Fluent interface, returns self
     */
    public function setCreator(Model_User $creator)
    {
        $this->_creator = $creator;
        return $this;
    }

    /**
     * Returns the creator of the user
     *
     * @return Model_User Creator of the user
     */
    public function getCreator()
    {
        if (!$this->_creator instanceof Model_User) {
            $this->setCreator(new Model_User());
        }
        return $this->_creator;
    }

    /**
     * Sets the id of the last updater
     *
     * @param integer|null $updaterId Id of the last updater
     * @return Model_User Fluent interface, returns self
     */
    public function setUpdaterId($updaterId = null) {
        if (!is_null($updaterId)) {
            $this->getUpdater()->setId($updaterId);
        }
        return $this;
    }

    /**
     * Returns the id of the updater
     *
     * @return integer|null
     */
    public function getUpdaterId()
    {
        return $this->getUpdater()->getId();
    }

    /**
     * Sets the last updater of the user
     *
     * @param Model_User $updater Last updater of the user
     * @return Model_User Fluent interface, returns self
     */
    public function setUpdater(Model_User $updater) {
        $this->_updater = $updater;
        return $this;
    }

    /**
     * Returns the last updater of the user
     *
     * @return Model_User Last updater of the user
     */
    public function getUpdater()
    {
        if (!$this->_updater instanceof Model_User) {
            $this->setUpdater(new Model_User());
        }
        return $this->_updater;
    }

    /**
     * Sets the person's model
     *
     * @param Model_Person $person
     * @return Model_User Fluent interface, returns self
     */
    public function setPerson(Model_Person $person)
    {
        $this->_person = $person;
        return $this;
    }

    /**
     * Returns the person's model
     *
     * @return Model_Person
     */
    public function getPerson()
    {
        if (!$this->_person instanceof Model_Person) {
            $this->setPerson(new Model_Person());
        }
        return $this->_person;
    }

    /**
     * Sets the person's ID
     *
     * @param integer $personId Person's ID
     * @return Model_User Fluent interface, returns self
     */
    public function setPersonId($personId)
    {
        $this->getPerson()->setId($personId);
        return $this;
    }

    /**
     * Returns the person's ID
     *
     * @return integer ID of the person
     */
    public function getPersonId()
    {
        return $this->getPerson()->getId();
    }

}
